package com.sa.user.service.impl;

/**
 *
 */
//@Service
//@RequiredArgsConstructor
//@Slf4j
//public class UserPointsTccServiceImpl implements UserPointsTccAction {
//
//    private final UserPointsMapper userPointsMapper;
//    private final RedisTemplate<String, Object> redisTemplate;
//    private static final String PREFIX = "TCC:UserPoints:";
//
//    @Override
//    public boolean prepare(BusinessActionContext context, UserPointsDTO userPointsDTO) {
//
//        String xid = RootContext.getXID();
//        log.info("Try phase started for transaction {}", xid);
//
//        // 幂等性检查
//        if (redisTemplate.hasKey(PREFIX + xid + ":status")) {
//            log.info("Transaction {} has already been processed, skipping", xid);
//            return true;
//        }
//
//        // 设置状态为 TRYING
//        redisTemplate.opsForValue().set(PREFIX + xid + ":status", TransactionStatus.TRYING.name(), 24, TimeUnit.HOURS);
//
//
//        // 将必要的信息放入上下文
//        if (context != null) {
//            context.addActionContext("userPointsDTO", JSON.toJSONString(userPointsDTO));
//        }
//
//        log.info("Try phase completed for transaction {}", xid);
//        return true;
//    }
//
//    @Override
//    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
//    public boolean commit(BusinessActionContext context) {
//        String xid = context.getXid();
//        log.info("Commit phase started for transaction {}", xid);
//
//        try {
//            // 获取状态
//            String status = (String) redisTemplate.opsForValue().get(PREFIX + xid + ":status");
//
//            // 幂等性检查
//            if (TransactionStatus.COMMITTED.name().equals(status)) {
//                log.info("Transaction {} has already been committed, skipping", xid);
//                return true;
//            }
//
//            // 处理业务逻辑，更新用户积分
//            UserPointsDTO userPointsDTO = JSON.parseObject(context.getActionContext("userPointsDTO").toString(), UserPointsDTO.class);
//            UserPoints userPoints = userPointsMapper.getUserPointsByUserId(userPointsDTO.getUserId());
//            if (userPoints == null) {
//                userPoints = new UserPoints();
//                userPoints.setUserId(userPointsDTO.getUserId());
//                userPoints.setPoints(userPointsDTO.getPoints());
//                userPointsMapper.insert(userPoints);
//            } else {
//                userPoints.setPoints(userPoints.getPoints() + userPointsDTO.getPoints());
//                userPointsMapper.updateById(userPoints);
//            }
//
//            // 更新状态为 COMMITTED
//            redisTemplate.opsForValue().set(PREFIX + xid + ":status", TransactionStatus.COMMITTED.name());
//
//            log.info("Commit phase completed for transaction {}", xid);
//            return true;
//        } catch (Exception e) {
//            log.error("Error in Commit phase for transaction {}", xid, e);
//            throw new RuntimeException("Commit failed", e);
//        }
//    }
//
//    @Override
//    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
//    public boolean rollback(BusinessActionContext context) {
//        String xid = context.getXid();
//        log.info("Rollback phase started for transaction {}", xid);
//
//        try {
//            // 获取状态
//            String status = (String) redisTemplate.opsForValue().get(PREFIX + xid + ":status");
//
//            // 幂等性检查和悬挂问题检查
//            if (status == null) {
//                log.info("No Try phase executed for transaction {}, skipping rollback", xid);
//                redisTemplate.opsForValue().set(PREFIX + xid + ":status", TransactionStatus.ROLLBACKED);
//                return true;
//            } else if (TransactionStatus.ROLLBACKED.name().equals(status)) {
//                log.info("Transaction {} has already been rollbacked, skipping", xid);
//                return true;
//            } else if (!TransactionStatus.TRYING.name().equals(status)) {
//                log.info("Transaction {} is in an invalid state {} for rollback, skipping", xid, status);
//                return true;
//            }
//
//            // 更新状态为 ROLLBACKED
//            redisTemplate.opsForValue().set(PREFIX + xid + ":status", TransactionStatus.ROLLBACKED.name());
//
//            log.info("Rollback phase completed for transaction {}", xid);
//            return true;
//        } catch (Exception e) {
//            log.error("Error in Rollback phase for transaction {}", xid, e);
//            throw new RuntimeException("Rollback failed", e);
//        }
//    }
//}


enum TransactionStatus {
    TRYING,
    COMMITTED,
    ROLLBACKED;
}

